﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
using CMS.BLL;

public class clsBasket
{
    private SortedList Items;



    public static clsBasket getclsBaskets(HttpContext context)
    {
        clsBasket clsBasket;
        //= New ShopCart()
        string strError;
        strError = context.Server.ToString();

        if (context.Session[Config.BASKETS] != null)
        {
            clsBasket = (clsBasket)context.Session[Config.BASKETS];
        }
        else
        {
            clsBasket = CreateclsBaskets(context);
        }
        return clsBasket;
    }

    public static clsBasket CreateclsBaskets(HttpContext context)
    {
        clsBasket clsBasketResult = new clsBasket();
        context.Session[Config.BASKETS] = clsBasketResult;
        return (clsBasket)context.Session[Config.BASKETS];
    }

    public static void Sigout(HttpContext context)
    {
        context.Session[Config.BASKETS] = null;
    }

    #region "Method of Cart"
    public clsBasket()
    {
        Items = new SortedList();
    }

    public void RemoveItem(int key)
    {
        Items.Remove(key);
    }

    public void AddItem(ItemclsBaskets item)
    {
        bool bExist;
        ItemclsBaskets itemNew;
        bExist = checkExistByKey(item.ProductID);
        if ((bExist))
        {
            Update(item);
        }
        else
        {
            // itemNew = item
            Items.Add(item.ProductID, item);
        }
    }
    public int getCountItem()
    {
        return Items.Count;
    }

    public ItemclsBaskets getItemByIndex(int index)
    {
        ItemclsBaskets item = null;
        item = (ItemclsBaskets)Items.GetByIndex(index);
        return item;
    }

    public void Empty()
    {
        this.clearItem();
    }

    public void Update(ItemclsBaskets item)
    {
        ItemclsBaskets itemNew;
        itemNew = this.getItemByKey(item.ProductID);
        //itemNew.Amount = itemNew.Amount + item.Amount
    }

    public DataTable getclsBasketDetail(int DefaultLangID, int CurrentLangID, int currentPageIndex, int nPageSize, ref int nPageCount)
    {
        int nCurentLang = Support.GetCurrentLanguage();
        int nDefaultLang = Support.GetDefaultLanguage();
        Products objProduct = new Products();
        DataTable dtbProduct = new DataTable();
      
        string sInclause;
        sInclause = this.getInclause();

        dtbProduct = objProduct.Search(0,sInclause,"",0,string.Empty,0,0,0,100000000000000,-1,-1,1,0,0,0,nDefaultLang,nCurentLang , currentPageIndex,nPageSize, ref nPageCount )  ;

        return dtbProduct;
    }

    // Get total money
    //public decimal getTotal()
    //{
    //    decimal decResult = 0;
    //    ItemShopCart item;
    //    IDictionaryEnumerator IDitem;

    //    IDitem = Items.GetEnumerator();

    //    while ((IDitem.MoveNext()))
    //    {
    //        item = (ItemShopCart)IDitem.Value;
    //        if ((item.TypePrice == InventoryType.Export))
    //        {

    //            decResult += item.Export;
    //        }
    //        else
    //        {
    //            decResult += item.Online;
    //        }
    //    }
    //    return decResult;
    //}

    #endregion

    #region "Private of member"

    private bool checkExistByKey(int key)
    {
        bool bResult = false;
        bResult = Items.ContainsKey(key);
        return bResult;
    }

    private ItemclsBaskets getItemByKey(int key)
    {
        ItemclsBaskets item = null;
        int intIndex = 0;
        intIndex = Items.IndexOfKey(key);
        if ((intIndex > -1))
        {
            item = (ItemclsBaskets)Items.GetByIndex(intIndex);
        }
        return item;
    }

    private void clearItem()
    {
        Items.Clear();
    }

    public string getInclause()
    {
        string strResult = "0";
        ItemclsBaskets item;
        IDictionaryEnumerator IDitem;

        IDitem = Items.GetEnumerator();

        while ((IDitem.MoveNext()))
        {
            item = (ItemclsBaskets)IDitem.Value;
            strResult += "," + item.ProductID.ToString();
        }
        return strResult;
    }
    #endregion

}

public class ItemclsBaskets
{
    int m_intProductID;
    #region "Method "
    public ItemclsBaskets(int ProductID)
    {
        m_intProductID = ProductID;
    }
    #endregion

    public int ProductID
    {
        get { return m_intProductID; }
        set { m_intProductID = value; }
    }
}
